//
//  XHBDynamicReplacementAppIconVC.m
//  iOS Object
//
//  Created by 保密 on 2019/6/27.
//  Copyright © 2019年 XiaoHaiBao. All rights reserved.
//  动态更换APPIcon

// 动态更换APPIcon <iOS 10.3之后，提供了相关的API来实现这个功能>

// 1. 添加APPIcon素材, 自定义一个文件夹导入项目就好, 不能放在 Assets.xcassets 里, 但是正常的主icon还是可以在这里设置的, 两者并不冲突

// 2. 配置info.plist中添加一行 Icon files（iOS 5）

    // 2.1 Primary Icon是设置app的主icon，可以在这里的Icon files数组内添加，有多个的话，依次添加，也可以这里不用填写，直接在Assets.xcassets 里配置；大多数情况都是在Assets.xcassets配置主Icon
    // 2.2 Newsstand Icon，暂时用不到，不用管

// 3. 在 Icon files（iOS 5）内添加一个 Key : CFBundleAlternateIcons, 类型为字典

    // 3.1 在CFBundleAlternateIcons这个字典里配置我们所有需要动态修改的icon, key为icon的名称, 类型为一个字典（这个字典里包含两个键: CFBundleIconFiles, 其值类型为Array, 内容为icon的名称; UIPrerenderedIcon的value是BOOL值。这个键值所代表的作用在iOS7之后（含iOS7）已失效, 在iOS6中可渲染app图标为带高亮效果。所以这个值目前我们可以不用关心。)

#import "XHBDynamicReplacementAppIconVC.h"
#import <SVProgressHUD.h>
#import <objc/runtime.h>

@interface XHBDynamicReplacementAppIconVC ()

@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;

@end

@implementation XHBDynamicReplacementAppIconVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"动态更换APPIcon";
    
    if (@available(iOS 10.3, *)) {
        // 如果alternateIconName为nil，则代表当前使用的是主图标.
        if (![UIApplication sharedApplication].alternateIconName) {
            self.iconImageView.image = [UIImage imageNamed:@"Logo"];
        }else {
            self.iconImageView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[UIApplication sharedApplication].alternateIconName ofType:@"png"]];
            MyLog(@"当前APPIcon : %@",[UIApplication sharedApplication].alternateIconName);
        }
    } else {
        MyLog(@"此功能iOS10.3后方可使用");
    }
    
}

- (IBAction)red:(UIButton *)button {
    [self changeAppIconWithName:@"red_icon"];
}

- (IBAction)yellow:(UIButton *)button {
    [self changeAppIconWithName:@"yellow_icon"];
}

- (IBAction)gerple:(UIButton *)button {
    [self changeAppIconWithName:@"gerple_icon"];
}

- (IBAction)orange:(UIButton *)button {
    [self changeAppIconWithName:@"orange_icon"];
}

- (IBAction)green:(UIButton *)button {
    [self changeAppIconWithName:@"green_icon"];
}

// 更改AppIcon
- (void)changeAppIconWithName:(NSString *)iconName {
    
    if (@available(iOS 10.3, *)) {
        // 如果为NO，表示当前进程不支持替换图标
        if (![[UIApplication sharedApplication] supportsAlternateIcons]) {
            return;
        }
        
        if ([iconName isEqualToString:@""]) {
            iconName = nil;
        }

        [[UIApplication sharedApplication] setAlternateIconName:iconName completionHandler:^(NSError * _Nullable error) {
            if (error) {
                NSLog(@"更换app图标发生错误了 ： %@",error);
            }else {
                self.iconImageView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[UIApplication sharedApplication].alternateIconName ofType:@"png"]];
            }
        }];
    } else {
        [SVProgressHUD showErrorWithStatus:@"此功能iOS10.3后方可使用"];
    }
}


/// 3D Touch效果上滑 底部显示按钮
- (NSArray<id<UIPreviewActionItem>> *)previewActionItems {
    
    UIPreviewAction *action1 = [UIPreviewAction actionWithTitle:@"分享" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSString *shareTitle = @"安慕希 - 希腊风味酸奶";
        UIImage *shareImage = [UIImage imageNamed:@"image_01"];
        NSURL *shareUrl = [NSURL URLWithString:@"https://www.baidu.com/"];
        NSArray *activityItems = @[shareTitle,
                                   shareImage,
                                   shareUrl]; // 必须要提供url 才会显示分享标签否则只显示图片
        
        UIActivityViewController *vc = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
        [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:vc animated:YES completion:^{
        }];
    }];
    
    UIPreviewAction *action3 = [UIPreviewAction actionWithTitle:@"取消" style:UIPreviewActionStyleDestructive handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        
    }];
    
    return @[action1,action3];
}

@end
